import * as types from "@/store/mutation_types";
import { userModel } from "@/api";
import Storage from "vue-ls";
import Config from "@/config/index";
import Vue from "vue";
import { Toast } from "vant";
Vue.use(Storage, {
  name: "ls"
}).use(Toast);
// import { dispatch } from "rxjs/internal/observable/pairs";
const tokenName = Config.request.tokenName;
const statusName = Config.response.statusName;

const actions = {
  /* 登陆 */
  doLogin({ commit }, formData) {
    return new Promise((resolve, reject) => {
      userModel
        ._doLogin(formData)
        .then(res => {
          if (res[statusName] === 200) {
            // 存储 token
            commit(types.LOGIN, res[tokenName]);
            // 改变登录状态为
            commit(types.LOGINSTATUS, true);
            // 存储账号密码
            commit("updateLoginInfo", formData);
            // 存储进 localStroage
            window.localStorage.setItem(tokenName, res[tokenName]);
            resolve(res);
          } else {
            if (res[Config.response.msgName]) {
              Toast.fail(res[Config.response.msgName]);
            }
          }
        })
        .catch(error => {
          console.log(error);
          reject(error);
        });
    });
  },
  /* 注册 */
  doSignup({ dispatch }, formData) {
    // return new Promise((resolve, reject) => {
    userModel
      ._doSignup(formData)
      .then(res => {
        if (res[statusName] === 200) {
          const { mobile, password } = formData;
          return dispatch("doLogin", {
            mobile,
            password
          });
        }
        // resolve(res);
      })
      .catch(error => {
        console.log(error);
        // reject(error);
      });
    // });
  },
  /* 获取用户信息 */
  getUserInfo({ commit }) {
    return new Promise((resolve, reject) => {
      userModel
        ._getUserInfo()
        .then(res => {
          const loginInfo = Vue.ls.get("loginInfo");
          if (res[statusName] === 200) {
            /* if (!res.receiver_area_name) {
              res.receiver_area_name = "";
            } */
            // 改变登录状态为
            commit(types.LOGINSTATUS, true);
            // 存储账号密码
            if (loginInfo) {
              commit("updateLoginInfo", loginInfo);
            }
            // 把 userInfo 存进 Vuex
            commit(types.USERINFO, res);
          }
        })
        .catch(error => {
          reject(error);
        });
    });
  },
  /* 退出登录 */
  logOut({ commit }) {
    return new Promise((resolve, reject) => {
      userModel
        ._logout()
        .then(res => {
          if (res[statusName] === 200) {
            // 清空userinfo
            commit(types.USERINFO, null);
            // 登录状态改为 false
            commit(types.LOGINSTATUS, false);
            // 清除 token
            commit(types.LOGIN, "");
            window.localStorage.removeItem(tokenName);
          }
        })
        .catch(error => {
          reject(error);
        });
    });
  }
};
export default actions;
